package com.sandasai.service.impl;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sandasai.dao.AdminDao;
import com.sandasai.dao.MarkDao;
import com.sandasai.dao.NoticeDao;
import com.sandasai.dao.SettingDao;
import com.sandasai.dao.StudentDao;
import com.sandasai.dao.TeacherDao;
import com.sandasai.dao.UserDao;
import com.sandasai.dao.WorkDao;
import com.sandasai.dao.WorktypeDao;
import com.sandasai.domain.Bankcard;
import com.sandasai.domain.Mark;
import com.sandasai.domain.Notice;
import com.sandasai.domain.Setting;
import com.sandasai.domain.Student;
import com.sandasai.domain.User;
import com.sandasai.domain.Work;
import com.sandasai.domain.Worktype;
import com.sandasai.service.StudentManager;
import com.sandasai.vo.StudentBean;
import com.sandasai.vo.WorkBean;
@Service("studentManager")
@Transactional
public class StudentManagerImpl implements StudentManager {
    @Resource
    private UserDao userDao;
    @Resource
    private StudentDao studentDao;
    @Resource
    private TeacherDao teacherDao;
    @Resource
    private AdminDao adminDao;
    @Resource
    private WorktypeDao worktypeDao;
    @Resource
    private WorkDao workDao;
    @Resource
    private MarkDao markDao;
    @Resource
    private SettingDao settingDao;
    @Resource
    private NoticeDao noticeDao;
    @Override
    public String getMessageOfDasai() {
        List<Notice> notices = noticeDao.findAll();
        if(notices.size() == 0) {
            return "";
        }else {
            return notices.get(notices.size() - 1).getContent();
        }
    }
    @Override
    public boolean addInfo(StudentBean studentBean) {
    	User user = userDao.findByUsername(studentBean.getUsername());
    	if(user == null) {
    		return false;
    	}
        Student student = user.getStudent();
        Bankcard bankCard = new Bankcard();
        bankCard.setBankCardNum(studentBean.getBankCardId());
        bankCard.setUser(user);
        user.setBankcard(bankCard);
        student.setCollege(studentBean.getCollege());
        student.setGrade(studentBean.getGrade());
        student.setOtherInfo(studentBean.getOtherInfo());
        student.setRealName(studentBean.getRealName());
        student.setStudentNo(studentBean.getStudentNo());
        student.setTel(studentBean.getTel());
        student.setVerified(false);
        studentDao.update(student);
        return true;
    }
    @Override
    public boolean submitExperienceText(String experienceText, String username) {
        User user = userDao.findByUsername(username);
        if(user == null) {
        	return false;
        }
        Student student = user.getStudent();
        student.setExperience(experienceText);
        userDao.update(user);
        return true;
    }
    @Override
    public StudentBean getStudentBean(String username) {
        User user = userDao.findByUsername(username);
        if(user == null) {
        	return null;
        }
        Student student = user.getStudent();
        StudentBean studentBean = new StudentBean();
        if(user.getBankcard() == null) {
        	studentBean.setBankCardId("");
        }else {
        	studentBean.setBankCardId(user.getBankcard().getBankCardNum());
        }
        studentBean.setCollege(student.getCollege());
        studentBean.setGrade(student.getGrade());
        studentBean.setOtherInfo(student.getOtherInfo());
        studentBean.setRealName(student.getRealName());
        studentBean.setStudentNo(student.getStudentNo());
        studentBean.setTel(student.getTel());
        return studentBean;
    }
    @Override
    public boolean updateInfo(StudentBean studentBean) {
        User user = userDao.findByUsername(studentBean.getUsername());
        if(user == null) {
        	return false;
        }
        Student student = user.getStudent();
        Bankcard bankCard;
        if(user.getBankcard() == null) {
        	bankCard = new Bankcard();
        }else {
        	bankCard = user.getBankcard();
        }
        bankCard.setBankCardNum(studentBean.getBankCardId());
        bankCard.setUser(user);
        user.setBankcard(bankCard);
        student.setCollege(studentBean.getCollege());
        student.setGrade(studentBean.getGrade());
        student.setOtherInfo(studentBean.getOtherInfo());
        student.setRealName(studentBean.getRealName());
        student.setStudentNo(studentBean.getStudentNo());
        student.setTel(studentBean.getTel());
        studentDao.update(student);
        return true;
    }
    @Override
    public WorkBean getWorkBean(String username) {
        User user = userDao.findByUsername(username);
        
        if(user == null) {
        	return null;
        }
        Student student = user.getStudent();
        if(student.getWork() == null) {
        	return null;
        }
        
        
        Work work = student.getWork();
        if(work == null) {
        	return null;
        }
        
        WorkBean workBean = new WorkBean();
        workBean.setDownloadURL("download.action?fileName=" + work.getFileName());
        workBean.setTitle(work.getTitle());
        
        workBean.setType(work.getWorktype().getTypeName());
        
        @SuppressWarnings("unchecked")
		List<Mark> marks = new LinkedList<Mark>(work.getMarks());
        
        if(student.getExperience() == null) {
            workBean.setTips("提交参赛经验后才可以查看成绩！");
        }
        else if(marks.size() != 2) {
            workBean.setTips("老师的评阅工作还没有完成！");
        }else {
            workBean.setComment1(marks.get(0).getComment());
            workBean.setComment2(marks.get(1).getComment());
            workBean.setScore1(marks.get(0).getScore());
            workBean.setScore2(marks.get(1).getScore());
            workBean.setAverage((marks.get(0).getScore() + marks.get(1).getScore()) / 2);
        }	
        return workBean;
    }
    @Override
    public Date getStartTimeOfSubmission() {
        List<Setting> settings = settingDao.findAll();
        if(settings.size() == 0) {
            return null;
        }else {
            return new Date(settings.get(settings.size() - 1).getStartTimeOfSubmission().getTime());
        }
    }
    @Override
    public Date getEndTimeOfSubmission() {
        List<Setting> settings = settingDao.findAll();
        if(settings.size() == 0) {
            return null;
        }else {
            return new Date(settings.get(settings.size() - 1).getEndTimeOfSubmission().getTime());
        }
    }
    @Override
    public boolean isWithinSubmissionTime() {
        List<Setting> settings = settingDao.findAll();
        if(settings.size() == 0) {
            return false;
        }else {
            Date startTimeOfSubmission = settings.get(settings.size() - 1).getStartTimeOfSubmission();
            Date endTimeOfSubmission = settings.get(settings.size() - 1).getEndTimeOfSubmission();
            Date currentTime = new Date();
            if(!currentTime.after(startTimeOfSubmission) || !currentTime.before(endTimeOfSubmission)) {
                return false;
            }
        }
        return true;
    }
    @Override
    public boolean addWork(WorkBean workBean) {
        String username = workBean.getOwner();
        User user = userDao.findByUsername(username);
        if(user == null) {
        	return false;
        }
        Student student = user.getStudent();
        Work work = student.getWork();
        if(work == null) {
            work = new Work();
            Worktype workType = worktypeDao.findById(Integer.parseInt(workBean.getType()));
            work.setWorktype(workType);
            work.setStudent(student);
            work.setVerified(false);
            work.setFileName(workBean.getFilePath());
            work.setTitle(workBean.getTitle());
            work.setAuthorName("" + user.getStudent().getRealName());
            workDao.save(work);
        }else {
            Worktype workType = worktypeDao.findById(Integer.parseInt(workBean.getType()));
            work.setWorktype(workType);
            work.setStudent(student);
            work.setVerified(false);
            work.setFileName(workBean.getFilePath());
            work.setTitle(workBean.getTitle());
            work.setAuthorName("" + user.getStudent().getRealName());
            workDao.update(work);
        }
        return true;
    }
    @Override
    public List<Worktype> getWorkTypes(){
        List<Worktype> worktypes = new LinkedList<Worktype>();
        worktypes.addAll(worktypeDao.findAll());
        return worktypes;
    }
    @Override
    public String getExperienceText(String username) {
        User user = userDao.findByUsername(username);
        return user.getStudent().getExperience();
    }
}